﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Chaunce.FrameWork.NetCore.Chaunce.Utility.Helpers
{
    public static partial class Extensions
    {
        public static string GetDisplayName(this Enum value)
        {
            var type = value.GetType();
            if (!type.IsEnum) throw new ArgumentException(String.Format("Type '{0}' is not Enum", type));

            var members = type.GetMember(value.ToString());
            if (members.Length == 0)
            {
                return "";
                //throw new ArgumentException(String.Format("Member '{0}' not found in type '{1}'", value, type.Name));
            }
            var member = members[0];
            var descriptionattributes = member.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (descriptionattributes.Length > 0)
            {
                return ((DescriptionAttribute)descriptionattributes[0]).Description;
            }
            else
            {
                var displayattributes = member.GetCustomAttributes(typeof(DisplayAttribute), false);
                if (displayattributes.Length > 0)
                {
                    return ((DisplayAttribute)displayattributes[0]).GetName();
                }
                else
                {
                    return "";
                }
            }
        }
        /// <summary>
        /// 自定义的一个属性类
        /// 目的：增加多个特性描述
        /// </summary>

    }

    public class SelfAttribute : Attribute
    {
        private string m_DisplayText1 = string.Empty;
        private string m_DisplayText2 = string.Empty;
        public SelfAttribute(string displayText1, string displayText2)
        {
            m_DisplayText1 = displayText1;
            m_DisplayText2 = displayText2;
        }

        public string DisplayText1
        {
            get { return m_DisplayText1; }
        }

        public string DisplayText2
        {
            get { return m_DisplayText2; }
        }
    }

    /// <summary>
    /// 注释
    /// </summary>
    [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
    public class MultipleDescription : Attribute
    {
        /// <summary>
        /// 得到 值，和对应值得描述
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static Dictionary<int, string> GetDescriptionValues<TEnum>() where TEnum : struct, IComparable, IConvertible, IFormattable
        {
            var result = new Dictionary<int, string>();
            var values = Enum.GetValues(typeof(TEnum));
            foreach (var value in values)
            {
                var field = value.GetType().GetField(value.ToString());
                var objs = field.GetCustomAttributes(typeof(MultipleDescription), false).FirstOrDefault();
                var text = ((MultipleDescription)objs).ZH + "," + ((MultipleDescription)objs).EN;
                result.Add((int)value, text);
            }
            result.OrderBy(item => item.Key);
            return result;
        }

        /// <summary>
        /// 得到枚举的中文
        /// </summary>
        /// <param name="enumType">枚举</param>
        /// <returns></returns>
        public static string GetZhDescription(object enumType)
        {
            var fieldinfo = enumType.GetType().GetField(enumType.ToString());
            var obj = fieldinfo.GetCustomAttributes(typeof(MultipleDescription), false).FirstOrDefault();
            return ((MultipleDescription)obj).ZH;
        }

        /// <summary>
        /// 得到枚举的英文
        /// </summary>
        /// <param name="enumType">枚举</param>
        /// <returns></returns>
        public static string GetEnDescription(object enumType)
        {
            var fieldinfo = enumType.GetType().GetField(enumType.ToString());
            var obj = fieldinfo.GetCustomAttributes(typeof(MultipleDescription), false).FirstOrDefault();
            return ((MultipleDescription)obj).ZH;
        }

        /// <summary>
        /// 中文
        /// </summary>
        public string ZH { get; } = "没有中文解释";
        /// <summary>
        /// 英文
        /// </summary>
        public string EN { get; } = "没有英文解释";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="zh"></param>
        public MultipleDescription(string zh)
        {
            ZH = zh;
        }
        /// <summary>
        /// 注释
        /// </summary>
        /// <param name="zh">中文</param>
        /// <param name="en">英文</param>
        public MultipleDescription(string zh, string en)
        {
            ZH = zh;
            EN = en;
        }
    }
}
